package com.ffcs.ffxtc.modules.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ffcs.ffxtc.modules.base.dao.DrawdbDataService;
import com.ffcs.ffxtc.modules.base.dao.ShareDataService;
import com.ffcs.ffxtc.modules.base.domin.entity.DrawdbData;
import com.ffcs.ffxtc.modules.base.domin.entity.ShareData;
import com.ffcs.utils.base.R;
import com.ffcs.utils.base.Request;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张正兴
 * @since 2024-04-30
 */
@RestController
@RequestMapping("/drawdbData")
@Tag(name = "数据存储表", description = "")
@RequiredArgsConstructor
public class DrawdbDataController {

    private final DrawdbDataService drawdbDataService;

    private final ShareDataService shareDataService;

    /**
     * 分页查询所有
     *
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public R<IPage<List<DrawdbData>>> findAllDrawdbDataPage(@RequestParam("drawdbUserId") Long drawdbUserId,
                                                            Request page) {
        return R.ok(drawdbDataService.findAllDrawdbDataPage(drawdbUserId, page));
    }

    /**
     * 分页查询
     *
     * @return
     */
    @GetMapping("/all")
    @Operation(summary = "全部查询")
    public R<List<DrawdbData>> findAllDrawdbData() {
        return R.ok(drawdbDataService.list());
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询")
    @Parameter(name = "id", description = "id")
    public R<DrawdbData> findOneDrawdbData(@PathVariable Long id) {
        return R.ok(drawdbDataService.getById(id));
    }

    /**
     * 新增
     *
     * @param drawdbData
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "新增")
    public R<Boolean> addDrawdbData(@RequestBody DrawdbData drawdbData) {
        return R.ok(drawdbDataService.save(drawdbData));
    }

    /**
     * 更新
     *
     * @param drawdbData
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "更新")
    public R<Boolean> updateDrawdbData(@RequestBody DrawdbData drawdbData) {
        return R.ok(drawdbDataService.updateById(drawdbData));
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/batch/{ids}")
    @Operation(summary = "删除")
    @Parameter(name = "id", description = "批量ID")
    @Transactional(rollbackFor = Exception.class)
    public R<Boolean> deleteBatchDrawdbData(@PathVariable("ids") List<Long> ids) {
        shareDataService.remove(new QueryWrapper<ShareData>().in(ShareData.DRAWDB_DATA_ID, ids));
        return R.ok(drawdbDataService.removeByIds(ids));
    }
}